package com.itheima.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.itheima.pojo.PictureSort;
import com.itheima.pojo.Result;
import com.itheima.pojo.StatusCode;
import com.itheima.service.FileService;
import com.itheima.service.PictureSortService;
import com.itheima.pojo.PictureSortVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/pictureSort")
public class PictureSortController {

    @Autowired
    private PictureSortService pictureSortService;

    /**
     * 添加分类
     * @param pictureSortVO
     */
    @RequestMapping("/add")
    public Result add(@RequestBody PictureSortVO pictureSortVO) {

        try {
            pictureSortService.add(pictureSortVO);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.ERROR,"插入失败");
        }
        return new Result(StatusCode.OK,"插入成功");
    }

    /**
     * 删除分类
     * @param pictureSortVO
     */
    @RequestMapping("/delete")
    public Result delete(@RequestBody PictureSortVO pictureSortVO) {

        try {
            pictureSortService.delete(pictureSortVO);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.ERROR,"删除失败");
        }
        return new Result(StatusCode.OK,"删除成功");
    }

    /**
     * 编辑分类
     * @param pictureSortVO
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody PictureSortVO pictureSortVO){

        try {
            pictureSortService.edit(pictureSortVO);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.ERROR,"更新失败");
        }
        return new Result(StatusCode.OK,"更新成功");
    }

    @Autowired
    private FileService fileService;
    
    /**
     * 获取图片分类列表
     * @param pictureSortVO
     * @return
     */
    @RequestMapping("/getList")
    public Map<String,Object> getList(@RequestBody PictureSortVO pictureSortVO){

        PageInfo<PictureSort> pageInfo = pictureSortService.getList(pictureSortVO);

        //封装data
        Map<String,Object> data=new HashMap<>();
        //封装列表数据
        List list = pageInfo.getList();
        List<Map> dataList=new ArrayList<>();
        for (Object info : list) {
            Map<String,Object>map = JSON.parseObject(JSON.toJSONString(info), Map.class);
            String picUrl = fileService.getPicUrlByUid((String) map.get("fileUid"));
            //将获取的urls封装成数组存入map
            List<String> photoList=new ArrayList<>();
            photoList.add(picUrl);
            map.remove("fileUid");
            map.put("photoList",photoList);
            dataList.add(map);
        }
        data.put("records",dataList);
        data.put("total",pageInfo.getTotal());
        data.put("size",pageInfo.getSize());
        data.put("current",pageInfo.getPageNum());
        data.put("orders",new ArrayList<>());
        data.put("optimizeCountSql",true);
        data.put("isSearchCount",true);

        //封装返回数据
        Map<String,Object> result=new HashMap<>();
        result.put("code","success");
        //将将data放入返回map
        result.put("data",data);

        return result;
    }

    /**
     * 通过Uid获取分类
     * @param pictureSortVO
     * @return
     */
    @RequestMapping("/getPictureSortByUid")
    public Map<String,Object> getPictureSortByUid(@RequestBody PictureSortVO pictureSortVO) {
        PictureSort pictureSort = pictureSortService.getPictureSortByUid(pictureSortVO);
        //封装返回数据
        Map<String,Object> result=new HashMap<>();
        result.put("code","success");
        result.put("data",pictureSort);
        return result;
    }

    /**
     * 置顶分类
     * @param pictureSortVO
     */
    @RequestMapping("/stick")
    public Result stick(@RequestBody PictureSortVO pictureSortVO) {

        try {
            pictureSortService.stick(pictureSortVO);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.ERROR,"置顶失败");
        }
        return new Result(StatusCode.OK,"置顶成功");
    }
}
